<?php

namespace api\finance\controller;

class AdminMoneyChargeController extends \Aaa\finance\controller\AdminMoneyChargeController {

    public $baseField = [
        [
            'field' => 'id',
            'name' => 'ID',
        ],
        [
            'field' => 'status',
            'name' => '状态',
            'type' => 'radio',
            'options' => [
                ['name' => '未知', 'value' => 0],
                ['name' => '失败', 'value' => 1],
                ['name' => '作废', 'value' => 2],
                ['name' => '成功', 'value' => 3],
                ['name' => '人工', 'value' => 3],
            ],
        ],
        [
            'field' => 'user_id',
            'name' => '用户',
        ],
        [
            'field' => 'bank',
            'name' => '支付平台',
        ],
        [
            'field' => 'amount',
            'name' => '充值金额',
        ],
        [
            'field' => 'amount_received',
            'name' => '到账金额',
        ],
        [
            'field' => 'create_time',
            'name' => '充值时间',
            'type' => 'time',
        ],
        [
            'field' => 'receive_time',
            'name' => '到账时间',
            'type' => 'time',
        ],
        [
            'field' => 'remark',
            'name' => '备注',
        ],
        [
            'field' => 'editor',
            'name' => '操作人',
        ],
        [
            'field' => 'subsite',
            'name' => '分站',
        ],
    ];

    public function beforeIndex() {
        $this->searchField = [
            'common' => [
                'id' => 'accurate',
                'status' => 'accurate',
                'user_id' => 'accurate',
                'bank' => 'accurate',
                'remark' => 'fuzzy',
                'editor' => 'accurate',
                'subsite' => 'accurate',
            ],
            'time' => ['create_time','receive_time',],
            'amount' => ['amount', 'amount_received',],
        ];
    }

    //魔宝支付 进件获取商户id
    public function mobaopayInput() {
        $data = $this->param;
        if ($data['method'] == 'alipay') {
            unset($data['business']);
        }
        if ($data['method'] == 'wxpay') {
            unset($data['bizCode']);
        }
        unset($this->param['method']);
        $data['signMsg'] = $this->mobaopaySign($data);
        $re = $this->post('https://trade2.mobaopay.com/cashier/api/gateway.cgi', $data);
        $re = json_decode($re);
        //print_r($re);
        if ($re->retCode == 1) {
            $this->success('进件成功');
        } else {
            $this->error($re->retMsg);
        }
    }

    //魔宝 签名
    private function mobaopaySign($data) {
        $config = cmf_get_option('system/api/mobaopay');
        $sign = [];
        ksort($data);
        foreach ($data as $k => $v) {
            $sign[] = "$k=$v";
        }
        $sign = implode('&', $sign);
        $key = file_get_contents(CMF_ROOT . $config['merchantCert']);
        if (openssl_pkcs12_read($key, $certs, $config['merchantCertPass'])) {
            if (openssl_sign($sign, $signature, $certs['pkey'], OPENSSL_ALGO_MD5)) {
                openssl_free_key($certs['pkey']);
                $sign = base64_encode($signature);
                return $sign;
            } else $this->error('签名错误');
        } else $this->error('证书或密码错误');
    }

}
